<template>
  <div class="aritlce-list">
    <van-pull-refresh
      v-model="refreshLoading"
      @refresh="getNextPageArtivle"
      success-text="刷新成功"
    >
      <van-list
        @load="getNextPageArtivle"
        :immediate-check="false"
        v-model="loading"
        :finished="finished"
        finished-text="没有更多了"
        :error.sync="error"
        error-text="请求失败，点击从新加载"
      >
        <article-item
          v-for="item in articles"
          :key="item.art_id"
          :aritcle-info="item"
        ></article-item>
      </van-list>
    </van-pull-refresh>
  </div>
</template>

<script>
// list组件属性
// - @load事件：距离底部小于offset的时候触发
// - offset：距离底部小于多少距离的时候触发load事件，默认值是300px
// - immdiate-check : ture : 是否加载后立即触发load事件
// - loading: 标识是否处于加载中
//   - 如果loding绑定false，@load事件就会被调用
//   -如果loading绑定true,@load事件就不会被调用
//   -每一次执行load事件，vant自动的把loading改为true
import { getArticleListAPI } from '@/api'
import ArticleItem from './ArticleItem.vue'
export default {
  name: 'ArticleList',
  props: {
    channelId: [Number, String]
  },
  components: {
    ArticleItem
  },
  data() {
    return {
      articles: [],
      preTimestamp: '',
      loading: false,
      finished: false,
      error: false,
      refreshLoading: false
    }
  },
  created() {
    this.getFirstPageArticle()
  },
  methods: {
    async getFirstPageArticle() {
      //时间戳：Date.now()  new Date().valueOf()
      try {
        const { data } = await getArticleListAPI(this.channelId, +new Date())
        this.articles = data.data.results
        this.preTimestamp = data.data.pre_timestamp
      } catch (error) {
        if (error.response?.status === 400) {
          this.$toast.fail(error.response.data.message)
          //保存第一页的下一页时间戳
        } else {
          throw error
        }
      }
    },
    async getNextPageArtivle() {
      try {
        //1.发送请求
        const { data } = await getArticleListAPI(
          this.channelId,
          this.preTimestamp
        )
        //2.添加数据
        const results = data.data.results
        //如果下滑加载，将数据放在最后面，如果下拉加载，放最前面
        if (this.loading) {
          this.articles = [...this.articles, ...results]
        } else {
          this.articles = [...results, ...this.articles]
        }
        //3.更新时间戳
        this.preTimestamp = data.data.pre_timestamp
        //4.判断是否加载完毕
        if (data.data.pre_timestamp === null) {
          this.finished = true
        }
      } catch (error) {
        this.error = true
      } finally {
        //5.手动将loafding改为false
        this.loading = false
        this.refreshLoading = false
      }
    }
    // getNextPageArtivle() {
    //   console.log(111);
    // },
  }
}
</script>

<style scoped lang="less">
//滚动条
//1.定高 2.overflow：auto/scroll/overlay

//less $代表 父元素的选择器
//css伪元素 ::after
// ::-webkit-scrollbar 滚动槽样式
// ::-webkit-scrollbar-thumb 滚动滑块样式

.aritlce-list {
  height: calc(100vh - 100px - 92px - 83px);
  overflow: auto;

  &::-webkit-scrollbar {
    width: 10px;
    background-color: transparent;
  }
  &::-webkit-scrollbar-thumb {
    background-color: blue;
    border-radius: 10px;
  }
}
</style>
